package com.wangzhenyu.learn.test.designPattern


//抽象工厂模式

interface Computer

class HuaWei : Computer

class Asus : Computer

class Xiaomi : Computer


class HuaWeiFactory : AbstractFactory() {
    override fun produce() = HuaWei()

    override fun toString(): String {
        return "HuaWeiFactory"
    }
}

class AsusFactory : AbstractFactory() {
    override fun produce() = Asus()

    override fun toString(): String {
        return "AsusFactory"
    }
}

class XiaomiFactory : AbstractFactory() {
    override fun produce() = Xiaomi()

    override fun toString(): String {
        return "XiaomiFactory"
    }
}

abstract class AbstractFactory {

    abstract fun produce(): Computer

    companion object Produce {
        inline operator fun <reified T : Computer> invoke(): AbstractFactory? {
            return when (T::class) {
                HuaWei::class -> {
                    HuaWeiFactory()
                }
                Asus::class -> {
                    AsusFactory()
                }
                Xiaomi::class -> {
                    XiaomiFactory()
                }
                else -> {
                    null
                }
            }
        }
    }

}